]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
BluetoothStatus: update only devices that actually changed
authorGiovanni Campagna <gcampagna@src.gnome.org>
Tue, 21 Dec 2010 15:26:26 +0000 (16:26 +0100)
committerGiovanni Campagna <gcampagna@src.gnome.org>
Thu, 23 Dec 2010 17:29:58 +0000 (18:29 +0100)
When receiving a "devices-changed" signal from BluetoothApplet,
check if some device item corresponds to an existing one, destroy
the remaining and add the new ones.
With this patch, signal emission when no device actually changed
(which happen due to bluetoothd creating temporary devices) result
in a no-op.

https://bugzilla.gnome.org/show_bug.cgi?id=637690

js/ui/status/bluetooth.js

index f2cfa8fba3037ba82f5c3399b2c43f8a811f0bba..47a7ed288c353007333bfc069399b84c9ce85bf6 100644 (file)
@@ -68,6 +68,7 @@ Indicator.prototype = {
         this._fullMenuItems = [new PopupMenu.PopupSeparatorMenuItem(),
                                new PopupMenu.PopupMenuItem(_("Send Files to Device...")),
                                new PopupMenu.PopupMenuItem(_("Setup a New Device..."))];
+        this._hasDevices = false;
         this._deviceSep = this._fullMenuItems[0]; // hidden if no device exists
 
         this._fullMenuItems[1].connect('activate', function() {
@@ -118,22 +119,56 @@ Indicator.prototype = {
         }
     },
 
-    _updateDevices: function() {
-        this._destroyAll(this._deviceItems);
-        this._deviceItems = [];
+    _deviceCompare: function(d1, d2) {
+        return d1.device_path == d2.device_path &&
+            d1.bdaddr == d2.bdaddr &&
+            d1.can_connect == d2.can_connect &&
+            d1.capabilities == d2.capabilities;
+    },
 
+    _updateDevices: function() {
         let devices = this._applet.get_devices();
-        let anydevice = false;
+
+        for (let i = 0; i < this._deviceItems.length; i++) {
+            let item = this._deviceItems.length;
+            let destroy = true;
+            for (let j = 0; j < devices.length; j++) {
+                // we need to deep compare because BluetoothSimpleDevice is a boxed type
+                // (but we take advantage of that, because _skip will disappear the next
+                // time get_devices() is called)
+                if (this._deviceCompare(item._device, devices[i])) {
+                    item.label.text = devices[i].alias;
+                    devices[i]._skip = true;
+                    destroy = false;
+                }
+            }
+            if (destroy) {
+                item.destroy();
+                item._destroyed = true;
+            }
+        }
+
+        let newlist = [ ];
+        for (let i = 0; i < this._deviceItems.length; i++) {
+            let item = this._deviceItems[i];
+            if (!item._destroyed)
+                newlist.push(item);
+        }
+        this._deviceItems = newlist;
+
+        this._hasDevices = newlist.length > 0;
         for (let i = 0; i < devices.length; i++) {
             let d = devices[i];
+            if (d._skip)
+                continue;
             let item = this._createDeviceItem(d);
             if (item) {
                 this.menu.addMenuItem(item, this._deviceItemPosition + this._deviceItems.length);
                 this._deviceItems.push(item);
-                anydevice = true;
+                this._hasDevices = true;
             }
         }
-        if (anydevice)
+        if (this._hasDevices)
             this._deviceSep.actor.show();
         else
             this._deviceSep.actor.hide();